
#include "exynos_4412.h"

/**********************************************************************
 * @brief       mydelay_ms program body
 * @param[in]   int (ms)
 * @return      None
 **********************************************************************/

#define  GPA1CON    (*(volatile unsigned int *)0x11400020)
#define  ULCON2     (*(volatile unsigned int *)0x13820000) 
#define  UCON2      (*(volatile unsigned int *)0x13820004) 
#define  UBRDIV2    (*(volatile unsigned int *)0x13820028) 
#define  UFRACVAL2  (*(volatile unsigned int *)0x1382002C) 
#define  UTXH2      (*(volatile unsigned int *)0x13820020) 
#define  UTRSTAT2   (*(volatile unsigned int *)0x13820010) 

void uart_init(void)
{
  //-----外: 配置管脚的工作模式
  GPA1CON = 0x22;  //配置GPA1_1 GPA10 为uart串口模式
  
  //-----内: 功能块设置
  //1.  uart 通讯协议格式的设置
  ULCON2 = 0x03;  //设置协议格式(  无校验位  1个停止位 8个数据位)
  UCON2 = 0x05;   //设置串口发送接收模式为polling模式
  
  /*2.  设置uart 的速度为115200
  DIV_VAL = (100000000/(115200 *16)) - 1 = 53.253
  UBRDIVn =  53
  UFRACVALn/16 = 0.253
  Therefore, UFRACVALn = 4
  */
  UBRDIV2 = 53;
  UFRACVAL2 = 4;
    
}

void putc(char c)
{
   while(1)
   {
      if(UTRSTAT2&0x02)  //检测发送是否为空
      {
         break;
      }
   }
   
   UTXH2 = c;  //发送字符
}

void mydelayms(int ms)
{
    int i, j;
    while(ms--)
    {
        for (i = 0; i < 5; i++)
            for (j = 0; j < 514; j++);
    }
}
void key_flag(int* flag)/*判断GPX1_1引脚的状态，即判断按键是否按下*/
{
	//static int flag = 0;
	if(!(GPX1.DAT & (0x1 << 1)))
		{
			mydelayms(10);
			if(!(GPX1.DAT & (0x1<<1)))    //二次检测，去抖
			{
				(*flag)++;
				//LED_ALL_ON();
			}			
			while(!(GPX1.DAT & (1 << 1)));
			//LED_ALL_OFF();

		}
}
void mydelay_ms(int ms,int* flag)
{
    int i, j;
    while(ms--)
    {
        for (i = 0; i < 5; i++)
            for (j = 0; j < 514; j++);
			key_flag(flag);
    }
}

void LED_ALL_ON()
{
	GPX2.DAT |= 0x1 << 7;    //Turn on LED2
	GPX1.DAT |= 0x1;
	GPF3.DAT |= (0x1 << 4);
	GPF3.DAT |= (0x1 << 5);
}
void LED_ALL_OFF()
{
	GPX2.DAT &= ~(0x1 << 7);    //Turn off LED2			
	GPX1.DAT &= ~0x1;//Turn off LED3
	GPF3.DAT &= ~(0x1 << 4);  //Turn off LED4
	GPF3.DAT &= ~(0x1 << 5);	//Turn off LED5
}
void LED_Water(int *flag)
{
	//while(flag!=2)
//	{
	/*
	GPX2.DAT &= ~(0x1 << 7);    //Turn off LED2
	mydelay_ms(500);			
	GPX1.DAT &= ~0x1;//Turn off LED3
	mydelay_ms(500);
	GPF3.DAT &= ~(0x1 << 4);  //Turn off LED4
	mydelay_ms(500);
	GPF3.DAT &= ~(0x1 << 5);	//Turn off LED5
	mydelay_ms(500);
	*/
	//Turn on LED2
        GPX2.DAT |= 0x1 << 7;
        mydelay_ms(500,flag);

        //Turn on LED3
        GPX1.DAT |= 0x1;
        //Turn off LED2
        GPX2.DAT &= ~(0x1<<7);
        mydelay_ms(500,flag);


        //Turn on LED4
        GPF3.DAT |= (0x1 << 4);
        //Turn off LED3
        GPX1.DAT &= ~0x1;
        mydelay_ms(500,flag);


        //Turn on LED5
        GPF3.DAT |= (0x1 << 5);
        //Turn off LED4
        GPF3.DAT &= ~(0x1 << 4);       
        mydelay_ms(500,flag);

		//Turn off LED5
        GPF3.DAT &= ~(0x1 << 5);
//	}
}
int main()
{
    int flag = 0;
	/*将GPX1_1设置成输入功能*/
    uart_init();

	GPX1.CON = GPX1.CON & (~(0xF << 4)); 
	
	GPX2.CON = (GPX2.CON & ~(0xf<<28))| 1<<28;//GPX2_7:output, LED2
	GPX1.CON = (GPX1.CON & ~(0xf)) | 1; //GPX1_0:output, LED3
	GPF3.CON = (GPX3.CON & ~(0xf<<16 | 0xf<<20)) | (1<<16 | 1<<20);//GPF3_4:output, LED4
                                                                   //GPF3_5:output, LED5
    LED_ALL_OFF();
	while(1)
	{

	 key_flag(&flag);
     putc(flag+'0');
	   
		if(flag == 1)
		{
			LED_ALL_ON();
		}
		else if(flag == 2)
		{
			LED_ALL_OFF();
			LED_Water(&flag);
		}
		else if(flag == 3)
		{
			flag = 0;
			LED_ALL_OFF();
		}
		
	//	if(flag == 1)
	//	LED_Water();
		
	/*	if(!(GPX1.DAT & (0x1 << 1)))
		{
			mydelayms(10);
			if(!(GPX1.DAT & (0x1<<1)))    //二次检测，去抖
			{
				 //LED_ALL_ON();
				 flag++;
			}			
			
			while(!(GPX1.DAT & (1 << 1)));
		}
	*/	

		/* if(key_flag() == 1)
		{
			
            LED_ALL_ON();
		}
		if(key_flag() == 2)
		{
			LED_ALL_OFF();
		}
		if(key_flag() == 3)
		{
            LED_Water();
		} */
		
	}
	return 0;
}
